package com.qianhui.dao;

import com.qianhui.common.IResult;
import com.qianhui.pojo.CommissionDayRecord;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;

/**
 * A data access object (DAO) providing persistence and search support for
 * CommissionDayRecord entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 *
 * @author MyEclipse Persistence Tools
 * @see com.qianhui.pojo.CommissionDayRecord
 */
public class CommissionDayRecordDAO extends BaseHibernateDAO
{
    private static final Logger log = LoggerFactory.getLogger(CommissionDayRecordDAO.class);
    // property constants
    public static final String BUYER_ID = "buyerId";
    public static final String TOTAL_BUYER_NUMBER = "totalBuyerNumber";
    public static final String DAY_ORDER_NUMBER = "dayOrderNumber";
    public static final String DAY_GOODS_NUMBER = "dayGoodsNumber";
    public static final String DAY_COMMISSION = "dayCommission";
    public static final String CURRENT_MONTH_COMMISSION = "currentMonthCommission";

    public CommissionDayRecord save(CommissionDayRecord transientInstance)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            getSession().saveOrUpdate(transientInstance);
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return transientInstance;
    }

    public List<CommissionDayRecord> saveList(List<CommissionDayRecord> list)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            for (CommissionDayRecord temp : list)
            {
                getSession().saveOrUpdate(temp);
            }
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return list;
    }

    public int delete(CommissionDayRecord persistentInstance)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            getSession().delete(persistentInstance);
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return IResult.DELETE_EXCEPTION;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return IResult.DELETE_SUCCESS;
    }

    public CommissionDayRecord findById(java.lang.String id)
    {
        log.debug("getting CommissionDayRecord instance with id: " + id);
        try
        {
            CommissionDayRecord instance = (CommissionDayRecord) getSession().get("com.qianhui.pojo.CommissionDayRecord", id);
            return instance;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List findByProperty(String propertyName, Object value)
    {
        log.debug("finding CommissionDayRecord instance with property: " + propertyName + ", value: " + value);
        try
        {
            String queryString = "from CommissionDayRecord as model where model." + propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);
            return queryObject.list();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List findByBuyerId(Object buyerId)
    {
        return findByProperty(BUYER_ID, buyerId);
    }

    public List findByTotalBuyerNumber(Object totalBuyerNumber)
    {
        return findByProperty(TOTAL_BUYER_NUMBER, totalBuyerNumber);
    }

    public List findByDayOrderNumber(Object dayOrderNumber)
    {
        return findByProperty(DAY_ORDER_NUMBER, dayOrderNumber);
    }

    public List findByDayGoodsNumber(Object dayGoodsNumber)
    {
        return findByProperty(DAY_GOODS_NUMBER, dayGoodsNumber);
    }

    public List findByDayCommission(Object dayCommission)
    {
        return findByProperty(DAY_COMMISSION, dayCommission);
    }

    public List findByCurrentMonthCommission(Object currentMonthCommission)
    {
        return findByProperty(CURRENT_MONTH_COMMISSION, currentMonthCommission);
    }

    public CommissionDayRecord findByBuyerIdAndRecordDate(String buyerId, Date recordDate)
    {
        try
        {
            String sql = "from CommissionDayRecord c where c.buyerId=:buyerId and c.recordDate=:recordDate";
            Query query = getSession().createQuery(sql);
            query.setParameter("buyerId", buyerId);
            query.setParameter("recordDate", recordDate);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return (CommissionDayRecord) tempList.get(0);
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public double getTotalCommissionByBuyerIdAndBeginTimeAndEndTime(String buyerId, Date beginTime, Date endTime)
    {
        try
        {
            String sql = "select sum(c.dayCommission) from CommissionDayRecord c where c.buyerId=:buyerId and recordDate between :beginTime and :endTime";
            Query query = getSession().createQuery(sql);
            query.setParameter("buyerId", buyerId);
            query.setDate("beginTime", beginTime);
            query.setDate("endTime", endTime);

            List li = query.list();
            if (li == null || li.isEmpty())
            {
                return 0;
            }
            else if (li.get(0) == null)
            {
                return 0;
            }
            else
            {
                return ((Number) li.get(0)).doubleValue();
            }

        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public int getTotalOrderNumberByBuyerIdAndBeginTimeAndEndTime(String buyerId, Date beginTime, Date endTime)
    {
        try
        {
            String sql = "select sum(c.dayOrderNumber) from CommissionDayRecord c where c.buyerId=:buyerId and recordDate between :beginTime and :endTime";
            Query query = getSession().createQuery(sql);
            query.setParameter("buyerId", buyerId);
            query.setDate("beginTime", beginTime);
            query.setDate("endTime", endTime);

            List li = query.list();
            if (li == null || li.isEmpty())
            {
                return 0;
            }
            else if (li.get(0) == null)
            {
                return 0;
            }
            else
            {
                return ((Number) li.get(0)).intValue();
            }

        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public int getTotalGoodsNumberByBuyerIdAndBeginTimeAndEndTime(String buyerId, Date beginTime, Date endTime)
    {
        try
        {
            String sql = "select sum(c.dayGoodsNumber) from CommissionDayRecord c where c.buyerId=:buyerId and recordDate between :beginTime and :endTime";
            Query query = getSession().createQuery(sql);
            query.setParameter("buyerId", buyerId);
            query.setDate("beginTime", beginTime);
            query.setDate("endTime", endTime);

            List li = query.list();
            if (li == null || li.isEmpty())
            {
                return 0;
            }
            else if (li.get(0) == null)
            {
                return 0;
            }
            else
            {
                return ((Number) li.get(0)).intValue();
            }

        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List getListByBuyerIdAndIndexAndPageSizeAndBeginTimeAndEndTime(String buyerId, int index, int pageSize, Date beginTime, Date endTime)
    {
        try
        {
            String sql = "from CommissionDayRecord c where c.buyerId=:buyerId and c.recordDate between :beginTime and :endTime order by c.recordDate desc";
            Query query = getSession().createQuery(sql);
            query.setParameter("buyerId", buyerId);
            query.setParameter("beginTime", beginTime);
            query.setParameter("endTime", endTime);
            query.setFirstResult((index - 1) * pageSize);
            query.setMaxResults(pageSize);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return tempList;
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public int getTotalCountByBuyerIdAndBeginTimeAndEndTime(String buyerId, Date beginTime, Date endTime)
    {
        try
        {
            String sql = "select count(*) from CommissionDayRecord c where c.buyerId=:buyerId and c.recordDate between :beginTime and :endTime";
            Query query = getSession().createQuery(sql);
            query.setParameter("buyerId", buyerId);
            query.setParameter("beginTime", beginTime);
            query.setParameter("endTime", endTime);
            int num = ((Number) query.iterate().next()).intValue();
            return num;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

}